package org.dromara.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.SysFloorBo;
import org.dromara.system.domain.vo.SysFloorVo;
import org.dromara.system.service.ISysFloorService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 楼栋管理
 *
 * @author LSL
 * @date 2024-05-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/floor")
public class SysFloorController extends BaseController {

    private final ISysFloorService sysFloorService;

    /**
     * 查询楼栋管理列表
     */
    @SaCheckPermission("system:floor:list")
    @GetMapping("/list")
    public TableDataInfo<SysFloorVo> list(SysFloorBo bo, PageQuery pageQuery) {
        return sysFloorService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出楼栋管理列表
     */
    @SaCheckPermission("system:floor:export")
    @Log(title = "楼栋管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysFloorBo bo, HttpServletResponse response) {
        List<SysFloorVo> list = sysFloorService.queryList(bo);
        ExcelUtil.exportExcel(list, "楼栋管理", SysFloorVo.class, response);
    }

    /**
     * 获取楼栋管理详细信息
     *
     * @param floorId 主键
     */
    @SaCheckPermission("system:floor:query")
    @GetMapping("/{floorId}")
    public R<SysFloorVo> getInfo(@NotNull(message = "主键不能为空")
                                 @PathVariable Long floorId) {
        return R.ok(sysFloorService.queryById(floorId));
    }

    /**
     * 新增楼栋管理
     */
    @SaCheckPermission("system:floor:add")
    @Log(title = "楼栋管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysFloorBo bo) {
        return toAjax(sysFloorService.insertByBo(bo));
    }

    /**
     * 修改楼栋管理
     */
    @SaCheckPermission("system:floor:edit")
    @Log(title = "楼栋管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysFloorBo bo) {
        return toAjax(sysFloorService.updateByBo(bo));
    }

    /**
     * 删除楼栋管理
     *
     * @param floorIds 主键串
     */
    @SaCheckPermission("system:floor:remove")
    @Log(title = "楼栋管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{floorIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] floorIds) {
        return toAjax(sysFloorService.deleteWithValidByIds(List.of(floorIds), true));
    }
}
